<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        body {
            background-color: black;
        }

        canvas {
            background-color: white;
        }
    </style>
    <script type="text/javascript">
        function atan(x, y) {
            return Math.atan(y / x) * 180 / Math.PI;
        }

        //检测是不是直线
        function checkIsLine(pointArray) {
            if (pointArray === null || pointArray === undefined || pointArray.length < 3) {
                return false;
            }
            // console.log(pointArray);
            var startX = pointArray[0].x;
            var startY = pointArray[0].y;

            var endX = pointArray[pointArray.length - 1].x;
            var endY = pointArray[pointArray.length - 1].y;

            var tan = atan(endX - startX, endY - startY);
            // console.log("tan" + tan);
            for (let i in pointArray) {
                // console.log(pointArray[i]);
                if (i > 4) {
                    var tantemp = atan(pointArray[i].x - pointArray[i - 4].x,
                        pointArray[i].y - pointArray[i - 4].y);
                    // console.log("tantemp" + tantemp);
                    if (Math.abs(tantemp - tan) > 16) {
                        return false;
                    }
                }
            }
            return true;
        }

        window.onload = function() {
            var oc = document.getElementById('c1');
            var ogc = oc.getContext('2d');
            oc.onmousedown = function(ev) {
                var pointArray = [];
                var ev = ev || window.event;
                ogc.moveTo(ev.clientX - oc.offsetLeft, ev.clientY - oc.offsetTop);
                document.onmousemove = function(ev) {
                    var point = new Object();
                    point.x = ev.clientX;
                    point.y = ev.clientY;
                    pointArray.push(point);
                    var ev = ev || window.event;
                    ogc.lineTo(ev.clientX - oc.offsetLeft, ev.clientY - oc.offsetTop);
                    ogc.stroke();
                };
                document.onmouseup = function() {
                    var isLine = checkIsLine(pointArray);
                    console.log("是否是直线:" + isLine);
                    document.onmousemove = null;
                    document.onmouseup = null;
                };

            }
        }
    </script>
</head>

<body>
    <canvas id="c1" width="400" height="400">
    </canvas>
</body>

</html>